package com.maxverse.boot.web2image.service.impl;

import com.maxverse.boot.web2image.core.service.SystemConfigService;
import com.maxverse.boot.web2image.storage.entity.SystemConfig;
import com.maxverse.boot.web2image.storage.repository.SystemConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    @Autowired
    private SystemConfigRepository systemConfigRepository;

    @Override
    public String getConfig(String key) {
        return systemConfigRepository.findByKey(key)
                .map(SystemConfig::getValue)
                .orElse(null);
    }

    @Override
    public String getConfig(String key, String defaultValue) {
        return systemConfigRepository.findByKey(key)
                .map(SystemConfig::getValue)
                .orElse(defaultValue);
    }

    @Override
    @Transactional
    public void setConfig(String key, String value) {
        setConfig(key, value, null);
    }

    @Override
    @Transactional
    public void setConfig(String key, String value, String description) {
        SystemConfig config = systemConfigRepository.findByKey(key)
                .orElse(new SystemConfig());
        
        config.setKey(key);
        config.setValue(value);
        config.setDescription(description);
        
        systemConfigRepository.save(config);
    }
} 